package com.define.ssh.action.statistical;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.model.Terminal;
import com.define.ssh.service.ClientVersionDataService;
import com.define.ssh.service.DataCountService;

@ParentPackage("admin")
public class ClientInfoAction extends BaseAdminAction {

	@Resource(name = "clientVersionDataServiceImpl")
	private ClientVersionDataService clientVersionDataService;
	
	
	private int pcCount=0;
	private int androidCount=0;
	private int iosCount=0;
	private int macCount=0;
	private int otherCount=0;
	
	private String clientType;
	
	private List<Terminal> terminalList;
	private int netid;
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		final DetachedCriteria dc = DetachedCriteria.forClass(Terminal.class);
		terminalList=this.clientVersionDataService.findByCriteria(dc);
		pcCount=this.getCount(null,"0");
		androidCount=this.getCount(null,"1");
		iosCount=this.getCount(null,"2");
		macCount=this.getCount(null,"3");
		otherCount=this.getCount(null,"4");
		return "clientInfo";
	}
	
	private int getCount(Integer netid,String cType){		
		String sql="SELECT SUM(t.count)  FROM `T_DATA_COUNT` t WHERE count_type=2 ";
		if (StringUtils.isNotBlank(cType)) {
			sql=sql+" AND t.`client_type`="+cType;
		}
		if(netid!=null){
			sql=sql+" AND t.`netid`="+netid;
		}
		List<BigDecimal> list=this.clientVersionDataService.findByQuery(sql);
		if(list.size()==1){
			BigDecimal obj=list.get(0);
			if(obj!=null){
				return obj.intValue();
			}
		}
		return 0;
	}
	
	public String countByVersion(){
		final StringBuffer sb = new StringBuffer();
		int count=this.getCount(netid,clientType);
		sb.append("{");
		sb.append("\"count\":"+count);
		sb.append("}");
		return ajax(JSONObject.fromObject(sb.toString()).toString());
	}



	public int getPcCount() {
		return pcCount;
	}

	public void setPcCount(int pcCount) {
		this.pcCount = pcCount;
	}

	public int getAndroidCount() {
		return androidCount;
	}

	public void setAndroidCount(int androidCount) {
		this.androidCount = androidCount;
	}

	public int getIosCount() {
		return iosCount;
	}

	public void setIosCount(int iosCount) {
		this.iosCount = iosCount;
	}

	public int getMacCount() {
		return macCount;
	}

	public void setMacCount(int macCount) {
		this.macCount = macCount;
	}

	

	public String getClientType() {
		return clientType;
	}

	public void setClientType(String clientType) {
		this.clientType = clientType;
	}

	public int getOtherCount() {
		return otherCount;
	}

	public void setOtherCount(int otherCount) {
		this.otherCount = otherCount;
	}

	public List<Terminal> getTerminalList() {
		return terminalList;
	}

	public void setTerminalList(List<Terminal> terminalList) {
		this.terminalList = terminalList;
	}

	public int getNetid() {
		return netid;
	}

	public void setNetid(int netid) {
		this.netid = netid;
	}

	
}